package cn.edu.nju.oraldefense.action;

import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.json.annotations.JSON;

import cn.edu.nju.oraldefense.model.Graduate;
import cn.edu.nju.oraldefense.model.GraduateOperation;
import cn.edu.nju.oraldefense.model.Teacher;
import cn.edu.nju.oraldefense.service.GraduateOperationService;
import cn.edu.nju.oraldefense.service.GraduateService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class GraduateListAction extends ActionSupport {

	private GraduateService graduateService;
	private GraduateOperationService graduateOperationService;
	private int start;
	private int limit;
	private String yearmonth;

	private List<Graduate> root;
	private int totalProperty;

	@JSON(serialize = false)
	public GraduateService getGraduateService() {
		return graduateService;
	}

	@Resource
	public void setGraduateService(GraduateService graduateService) {
		this.graduateService = graduateService;
	}

	@JSON(serialize = false)
	public GraduateOperationService getGraduateOperationService() {
		return graduateOperationService;
	}

	@Resource
	public void setGraduateOperationService(
			GraduateOperationService graduateOperationService) {
		this.graduateOperationService = graduateOperationService;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public String getYearmonth() {
		return yearmonth;
	}

	public void setYearmonth(String yearmonth) {
		this.yearmonth = yearmonth;
	}

	public List<Graduate> getRoot() {
		return root;
	}

	public void setRoot(List<Graduate> root) {
		this.root = root;
	}

	public int getTotalProperty() {
		return totalProperty;
	}

	public void setTotalProperty(int totalProperty) {
		this.totalProperty = totalProperty;
	}

	/**
	 * 教师查询所指导的学生的列表
	 * 
	 * @return
	 */
	public String teacherExecute() {
		System.out.println("GraduateListAction.teacherExecute()");
		ActionContext ctx = ActionContext.getContext();
		Teacher teacher = (Teacher) ctx.getSession().get("teacher");

		if (teacher != null) {
			root = graduateService.getByTeacherAndPage(teacher, start, limit);
			totalProperty = graduateService.getCountByTeacher(teacher);
		}
		return SUCCESS;
	}

	/**
	 * 教务员返回所有学生的列表
	 */
	public String deanExecute() {
		System.out.println("start:" + start + ";limit:" + limit);
		root = graduateService.getByPage(start, limit);
		totalProperty = graduateService.getCount();
		return SUCCESS;
	}

	/**
	 * 管理员返回所有学生列表
	 */
	public String observerExecute() {
		String ym[] = yearmonth.split("-");
		int year = Integer.parseInt(ym[0]);
		int month = Integer.parseInt(ym[1]);
		yearmonth = yearmonth + "-01";

		String nextyearmonth = "";
		if (month == 12) {
			year++;
			nextyearmonth = year + "-" + "01" + "-01";
		} else {
			nextyearmonth = year + "-" + (month + 1) + "-01";
		}

		root = graduateService.getByPage(start, limit);
		for (Graduate graduate : root) {
			graduate.setCommitTime(graduateOperationService
					.getByGraduateAndTypeAndMonth(graduate,
							GraduateOperation.OPERATION_ADD, yearmonth,
							nextyearmonth).size());
			graduate.setModifyTime(graduateOperationService
					.getByGraduateAndTypeAndMonth(graduate,
							GraduateOperation.OPERATION_MODIFY, yearmonth,
							nextyearmonth).size());
		}
		totalProperty = graduateService.getCount();
		return SUCCESS;
	}
}
